package org.vartist.atld.menu.task.util.base64;

import java.io.IOException;

import org.vartist.atld.menu.activity.util.base64.Base64Activity;
import org.vartist.atld.util.base64.Base64Util;
import org.vartist.atld.util.image.ImageUtil;
import org.vartist.atld.util.log.LogUtil;

import android.graphics.Bitmap;
import android.os.AsyncTask;

public class ConvertBase64ToImageTask extends AsyncTask<String, Integer, Long> {

	private Base64Activity mActivity = null;
	private String mEncodedText = null;
	private Bitmap mReversedBitmap = null;
	
	public ConvertBase64ToImageTask(Base64Activity activity, String encodedText) {
		mActivity = activity;
		mEncodedText = encodedText;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		mActivity.setProgressVisibility(true);
	}
	
	@Override
	protected Long doInBackground(String... params) {
		if (mEncodedText != null && mEncodedText.length() > 0) {
			byte[] byteFile = null;
			
			try {
				byteFile = Base64Util.decode(mEncodedText);
				mReversedBitmap = ImageUtil.byteArrayToBitmap(byteFile);
				
			} catch (IOException e) {
				LogUtil.e(e.getMessage());
			} finally {
				byteFile = null;
			}
		}
		
		return null;
	}

	@Override
	protected void onPostExecute(Long result) {
		super.onPostExecute(result);
		mActivity.setReverseConvertedBitmap(mReversedBitmap);
		mActivity.setProgressVisibility(false);
	}
}
